home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Snippets / Toolbox / Reinstallable / LaunchInits.c next >
Encoding:
C/C++ Source or Header  |  1993-10-07  |  4.6 KB  |  189 lines  |  [TEXT/MPS ]

  1. /*
  2.  *  LaunchInits application
  3.  *  v 1.0
  4.  *
  5.  *  executes the code in each init passed in via the
  6.  *  OpenDocuments event
  7.  *
  8.  *  June 1993  Greg Robbins
  9.  *  Developer Technical Support
  10.  *
  11.  */
  12.  
  13. #include "AppleEvents.h"
  14. #include "GestaltEqu.h"
  15. #include "Resources.h"
  16.  
  17. // prototypes
  18. pascal OSErr DoAEOpenApplication(AppleEvent *, AppleEvent *,long);
  19. pascal OSErr DoAEOpenDocuments(AppleEvent *, AppleEvent *,long);
  20. pascal OSErr DoAEPrintDocuments(AppleEvent *, AppleEvent *,long);
  21. pascal OSErr DoAEQuitApplication(AppleEvent *, AppleEvent *,long);
  22. void InitAppleEventsStuff(void);
  23. void DoHighLevelEvent(EventRecord *);
  24. void RunInitCode(FSSpecPtr);
  25.  
  26.  
  27. // globals
  28.  
  29. Boolean gAppleEventsFlag, gQuitFlag;
  30. long gSleepVal;
  31.  
  32. // RunInitCode loads and executes all INIT resource code
  33. // in the file
  34.  
  35. pascal void DoJSR(ProcPtr p)
  36. = { 0x205F, 0x4E90 };  // MOVEA.L (A7)+, A0; JSR (A0)
  37.  
  38. void RunInitCode(FSSpecPtr fileFSSpecPtr)
  39. {
  40.     OSErr retCode;
  41.     short resRefNum;
  42.     short index;
  43.     Handle initHandle;
  44.     
  45.     resRefNum = FSpOpenResFile(fileFSSpecPtr, fsRdPerm);
  46.     if (resRefNum != -1) {
  47.         for (index = 1; index <= CountResources('INIT'); index++) {
  48.             initHandle = Get1IndResource('INIT', index);
  49.             if (initHandle != nil) {
  50.                 HLock(initHandle);
  51.                 DoJSR((ProcPtr) *initHandle);
  52.             }
  53.         }
  54.         CloseResFile(resRefNum);
  55.     }        
  56. }
  57.  
  58. // Apple event handlers to be installed
  59.  
  60. pascal OSErr DoAEOpenApplication(AppleEvent * theAppleEvent,
  61.                                  AppleEvent * replyAppleEvent, 
  62.                                  long refCon)
  63. {
  64. #pragma unused (theAppleEvent, replyAppleEvent, refCon)
  65.     gQuitFlag = true;
  66.     return noErr;
  67. }
  68.  
  69. pascal OSErr DoAEOpenDocuments(AppleEvent * theAppleEvent,
  70.                                AppleEvent * replyAppleEvent, 
  71.                                long refCon)
  72. {
  73. #pragma unused (replyAppleEvent, refCon)
  74.  
  75.     OSErr retCode;
  76.     AEDescList docDescList;
  77.     long docCount, index;
  78.     AEKeyword keyword;
  79.     DescType returnedType;
  80.     FSSpec fileFSSpec;
  81.     Size actualSize;
  82.     
  83.     retCode = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList,
  84.         &docDescList);
  85.     if (retCode == noErr) {
  86.         
  87.         if (AECountItems(&docDescList, &docCount) == noErr) {
  88.             
  89.             for (index = 1; index <= docCount; index++) {
  90.                 retCode = AEGetNthPtr(&docDescList, index, typeFSS,
  91.                     &keyword, &returnedType, (Ptr) &fileFSSpec, 
  92.                     sizeof(FSSpec), &actualSize);
  93.                 if (retCode == noErr) {
  94.                     RunInitCode(&fileFSSpec);
  95.                 }
  96.             }
  97.         }
  98.         (void) AEDisposeDesc(&docDescList);
  99.     }
  100.  
  101.     gQuitFlag = true;
  102.     return noErr;
  103. }
  104.  
  105. pascal OSErr DoAEPrintDocuments(AppleEvent * theAppleEvent,
  106.                                 AppleEvent * replyAppleEvent, 
  107.                                 long refCon)
  108. {
  109. #pragma unused (theAppleEvent, replyAppleEvent, refCon)
  110.     gQuitFlag = true;
  111.     return errAEEventNotHandled;
  112. }
  113.  
  114. pascal OSErr DoAEQuitApplication(AppleEvent * theAppleEvent,
  115.                                  AppleEvent * replyAppleEvent, 
  116.                                  long refCon)
  117. {
  118. #pragma unused (theAppleEvent, replyAppleEvent, refCon)
  119.     gQuitFlag = true;
  120.     return noErr;
  121. }
  122.  
  123. void InitAppleEventsStuff(void)
  124. // install Apple event handlers
  125. {
  126.     OSErr retCode;
  127.     
  128.     retCode = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
  129.                 (EventHandlerProcPtr) DoAEOpenApplication, 0, false);
  130.                                     
  131.     if (retCode == noErr)
  132.         retCode = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
  133.                 (EventHandlerProcPtr) DoAEOpenDocuments, 0, false);
  134.  
  135.     if (retCode == noErr)
  136.         retCode = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
  137.                 (EventHandlerProcPtr) DoAEPrintDocuments, 0, false);
  138.     if (retCode == noErr)
  139.         retCode = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  140.                 (EventHandlerProcPtr) DoAEQuitApplication, 0, false);
  141.     
  142.     if (retCode != noErr) DebugStr("\pInstall event handler failed");
  143.  
  144. }
  145.  
  146. void DoHighLevelEvent(EventRecord * theEventRecPtr)
  147. // high-level event dispatching
  148. {
  149.     (void) AEProcessAppleEvent(theEventRecPtr);
  150. }
  151.  
  152. main()
  153. {
  154.     OSErr retCode;
  155.     long gestResponse;
  156.     
  157.     EventRecord mainEventRec;
  158.     Boolean eventFlag;
  159.     
  160.     // initialize QuickDraw globals
  161.     
  162.     InitGraf(&qd.thePort);
  163.     
  164.     // initialize application globals
  165.     
  166.     gQuitFlag = false;
  167.     gSleepVal = 10;     // a brief, non-zero sleep time is appropriate
  168.     
  169.     // is the Apple Event Manager available?
  170.     retCode = Gestalt(gestaltAppleEventsAttr, &gestResponse);
  171.     if (retCode != noErr ||
  172.         (gestResponse & (1 << gestaltAppleEventsPresent)) == 0)
  173.  
  174.         gQuitFlag = true; // bail if no Apple Events
  175.  
  176.     // install Apple event handlers
  177.     else
  178.         InitAppleEventsStuff();
  179.     
  180.     // main event loop
  181.     
  182.     while (!gQuitFlag) {
  183.         eventFlag = WaitNextEvent(everyEvent, &mainEventRec, gSleepVal, nil);
  184.         
  185.         if (mainEventRec.what == kHighLevelEvent)
  186.             DoHighLevelEvent(&mainEventRec);
  187.     }
  188. }
  189.